package org.ax.service.device.imple;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ax.mapper.device.DeviceCheckMapper;
import org.ax.pojo.device.DeviceCheck;
import org.ax.service.device.DeviceCheckService;
import org.ax.service.device.DeviceService;
import org.ax.service.employ.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 编程人:赵浩鑫
 * 时间:2021-03-03-14:13
 */
@Service
public class IDeviceCheckService extends ServiceImpl<DeviceCheckMapper, DeviceCheck> implements DeviceCheckService {
    @Autowired
    DeviceService deviceService;
    @Autowired
    EmployeeService employeeService;
    @Override
    public Page<DeviceCheck> searchDevices(Page page, String deviceId) {
        QueryWrapper<DeviceCheck> queryWrapper=new QueryWrapper<DeviceCheck>();
        queryWrapper.eq(!StringUtils.isBlank(deviceId), "device_id",deviceId);
        Page page1 = getBaseMapper().selectPage(page, queryWrapper);
        List<DeviceCheck> deviceChecks=(List<DeviceCheck>)page1.getRecords();
        for (DeviceCheck deviceCheck : deviceChecks) {
            deviceCheck.setDevice(deviceService.toDeviceId(deviceCheck.getDeviceId()));
            deviceCheck.setEmployee(employeeService.toEmployeeId(deviceCheck.getDeviceCheckEmpId()));
        }
        page1.setRecords(deviceChecks);
        return page1;
    }

    @Override
    public boolean addDeviceCheck(DeviceCheck deviceCheck) {
        return getBaseMapper().insert(deviceCheck)>0;
    }

    @Override
    public boolean updateDeviceCheck(DeviceCheck deviceCheck) {
        return getBaseMapper().updateById(deviceCheck)>0;
    }

    @Override
    public boolean deletesDeviceCheck(String[] ids) {
        return getBaseMapper().deleteBatchIds(Arrays.asList(ids))>0;
    }

    @Override
    public DeviceCheck toDeviceCheckId(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public List<DeviceCheck> findDeviceChecks() {
        return getBaseMapper().selectList(null);
    }
}
